package com.beeantec.jnrerc.modules.evaluate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.request.EvaluateDetailDTO;
import com.beeantec.jnrerc.dto.request.EvaluateReqDTO;
import com.beeantec.jnrerc.dto.request.ExamSaveReqDTO;
import com.beeantec.jnrerc.entity.Evaluate;
import com.beeantec.jnrerc.modules.evaluate.service.EvaluateService;
import com.beeantec.jnrerc.modules.exam.service.ExamRepoService;
import com.beeantec.jnrerc.modules.exam.service.ExamService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags={"评价"})
@RestController
@RequestMapping("/api/evaluate")
public class EvaluateController extends BaseController {

    @Autowired
    private EvaluateService baseService;

    @Autowired
    private MemberService memberService;


    /**
     * 添加或修改
     *
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public ApiRest<String> save(@RequestHeader("token") String token, @RequestBody EvaluateReqDTO reqDTO) {
        //复制参数
        MemberVo memberVo = memberService.token(token);
        Evaluate evaluate = new Evaluate();
        BeanUtils.copyProperties(reqDTO,evaluate);
        evaluate.setMemberId(memberVo.getMemberId());
        if (baseService.save(evaluate)){
            return success("评价成功");
        }
        return failure("评价失败");
    }




    /**
     * 获取详情
     *
     * @return
     */
    @ApiOperation(value = "获取详情")
    @RequestMapping(value = "/detail", method = {RequestMethod.GET})
    public ApiRest<EvaluateDetailDTO> get(@RequestHeader("token")String token, @RequestParam("type")Integer type, @RequestParam("itemId")Integer itemId) {
        MemberVo memberVo = memberService.token(token);
        EvaluateDetailDTO evaluateDetailDTO = new EvaluateDetailDTO();
        QueryWrapper<Evaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Evaluate::getType,type)
                .eq(Evaluate::getItemId,itemId);
        List<Evaluate> evaluateList = new ArrayList<>();
        evaluateList = baseService.list(queryWrapper);
        double score = 0.0;
        for (Evaluate evaluate:evaluateList){
            if (evaluate.getMemberId().equals(memberVo.getMemberId())){
                evaluateDetailDTO.setHasEva(1);
            }else {
                evaluateDetailDTO.setHasEva(0);
            }
            score = score+evaluate.getScore();
        }
        Double avg = score/evaluateList.size();
        evaluateDetailDTO.setScore(avg);
        return super.success(evaluateDetailDTO);
    }
}